package com.freshcity.page.home
{
	import com.freshcity.component.container.Component;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.graphics.VoiceIcon;
	import com.freshcity.core.Dispatcher;
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	public class SoundPanel extends Component
	{
		private var voiceIcon:VoiceIcon;
		private var isPlay:Boolean;
		
		public function SoundPanel(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this.mouseEnabled = false;
			super(parent, xpos, ypos);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			voiceIcon = new VoiceIcon();
			voiceIcon.buttonMode = true;
			addChild(voiceIcon);
			
			Dispatcher.addEventListener(EventName.PlaySound,onPlaySound);
			Dispatcher.addEventListener(EventName.CompleteSound,onCompleteSound);
		}
		
		private function checkStatus():void
		{
			voiceIcon.voice.x = 13;
			voiceIcon.voice.width = 3;
			voiceIcon.voice.height = 7.5;
			
			if(isPlay){
				onPlaySound();
			}
		}
		
		private function onCompleteSound(e:Event):void
		{
			isPlay = false;
		}
		
		private function onPlaySound(evt:Event=null):void
		{
			if(evt)isPlay = true;
			var myfun:Function=function():void{
				TweenLite.delayedCall(0.3,checkStatus);
			};
			TweenLite.to(voiceIcon.voice,1,{x:22,width:8,height:20,onComplete:myfun});
		}
		
		override public function draw():void
		{
			super.draw();
		}
		
	}
}